<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_school_main extends school_main {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 	取出一条信息
     */

    public function do_detail($sm_id = '') {
        global $_G;
        $detail = array();
        $sm_id = isset($_REQUEST['sm_id']) ? $_REQUEST['sm_id'] : '';
        $detail = $this->one_info('school_main', 'sm_id', $sm_id);
        //DEBUG 获取课程三级分类 结束
        //DEBUG 调试输出
        $return['code'] = '0';
        $return['data'] = $data;
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        //include template('course/main/detail');
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $school_main = isset($_REQUEST['school_main']) && !empty($_REQUEST['school_main']) ? $_REQUEST['school_main'] : '';
        if ('1' == $issubmit) {
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach ($school_main AS $key => $value) {
                if (is_array($value) && !empty($value)) {
                    $school_main[$key] = implode(",", $value);
                }
            }
            $school_main['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('school_main', $school_main);
            if ($insert_id) {
                showmessage('操作成功', 'index.php?mod=school&action=main&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=school&action=main&do=index');
            }
        } else {
            include template('course/main/add');
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        $issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit'] : '';
        $school_main = isset($_REQUEST['school_main']) && !empty($_REQUEST['school_main']) ? $_REQUEST['school_main'] : '';
        $sm_id = isset($_REQUEST['sm_id']) ? $_REQUEST['sm_id'] : '';
        if ($sm_id) {
            if ('1' == $issubmit) {
                $school_main['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach ($school_main AS $key => $value) {
                    if (is_array($value) && !empty($value)) {
                        $school_main[$key] = implode(",", $value);
                    }
                }
                $where = array('sm_id' => $sm_id);
                $effect_row = $this->edit('school_main', $school_main, $where);
                if ($effect_row) {
                    showmessage('操作成功', 'index.php?mod=school&action=main&do=index');
                } else {
                    showmessage('操作失败', 'index.php?mod=school&action=main&do=index');
                }
            } else {
                //DEBUG 获取操作对象信息
                $info = $this->one_info('school_main', 'sm_id', $sm_id);
                include template('course/main/edit');
            }
        } else {
            showmessage('请选择编辑对象', 'index.php?mod=school&action=main&do=index');
        }
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        $sm_id = isset($_REQUEST['sm_id']) ? $_REQUEST['sm_id'] : '';
        if ($sm_id) {
            $effect_row = $this->delete("school_main", array("sm_id" => $sm_id), $limit = 1);
            if ($effect_row) {
                showmessage('操作成功', 'index.php?mod=school&action=main&do=index');
            } else {
                showmessage('操作失败', 'index.php?mod=school&action=main&do=index');
            }
        }
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';
        $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        $page = max(1, intval($_REQUEST['page']));
        $default_limit = 200;
        $perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : $default_limit;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql .= " AND cm_name LIKE '%" . $keyword . "%' ";
        }
        $k = isset($_REQUEST['k']) ? $_REQUEST['k'] : '';
        $l = isset($_REQUEST['l']) ? $_REQUEST['l'] : '';
        if ($k && $l) {
            $wheresql .= " AND am_id_" . $l . " = '" . $k . "' AND isdelete=0 ";
        }
        $orderby = " ORDER BY sm_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        $page_result = $this->index('school_main', $page_condition);
        $school_main = array();
        if ($client == 'pc') {
            $sm_all = array(
                array('value'=>'-1','p'=>'0','label'=>'小学'),
                array('value'=>'-2','p'=>'0','label'=>'初中'),
                array('value'=>'-3','p'=>'0','label'=>'高中'),
                array('value'=>'-4','p'=>'0','label'=>'一贯制')
            );
            foreach ($page_result['page_data'] AS $key => $value) {
                $tmp = array();
                $tmp['value'] = $value['sm_id'];
                $tmp['p'] = '-'.$value['sm_type'];
                $tmp['label'] = $value['sm_name'];
                $sm_all[] = $tmp;
            }
        }
        //DEBUG 格式化树形数组
        $sm_all=$this->arr2tree_pc($sm_all);
        if ($_G['gp_api']) {
            $return['code'] = '0';
            if ($client == 'pc') {
                $return['data'] = $sm_all;
            } else {
                $return['data'] = $sm_all;
            }
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=school&action=main&do=index&keyword=".$keyword);
            //include template('course/main/index');	  
        }
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE sm_name LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY sm_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('school_main', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>